package com.panopset.rdb;

/**
 * Database backed properties.
 *
 * @author Karl Dinwiddie
 *
 */
public final class DataProps {

    /**
     * Store.
     */
    private final Store store;

    public Store getStore() {
        return store;
    }
    /**
     * key.
     */
    private static final String KEY = "key";

    /**
     * val.
     */
    private static final String VAL = "val";

    /**
     * Constructor.
     * @param Store tableStore
     * @param tableSchema
     *            Table schema.
     * @param tableName
     *            Table name.
     */
    public DataProps(final Store tableStore,
            final String tableSchema, final String tableName) {
        store = tableStore;
        schema = tableSchema;
        name = tableName;
    }

    /**
     * Schema.
     */
    private String schema;

    /**
     * Table name.
     */
    private String name;

    /**
     * Property table.
     */
    private Table table;

    /**
     * @return Property table.
     */
    private Table getTable() {
        if (table == null) {
            table = new Table(schema, name);
            table.setPrimaryKey(new FieldString4096(KEY));
            table.addField(new FieldString4096(VAL));
        }
        return table;
    }

    /**
     *
     * @param key
     *            Key.
     * @param value
     *            Value.
     */
    public void put(final String key, final String value) {
        store.put(getTable(), key, value);
    }

    /**
     *
     * @param key
     *            Key.
     * @return value from key.
     */
    public String get(final String key) {
        Object[] objs = store.get(getTable(), key);
        if (objs != null && objs.length > 0) {
            return objs[0].toString();
        }
        return null;
    }

}
